package net.starwifi.duobao.service.impl;


import net.starwifi.duobao.entity.*;
import net.starwifi.duobao.mvc.Filter;
import net.starwifi.duobao.mvc.Page;
import net.starwifi.duobao.mvc.PageView;
import net.starwifi.duobao.repository.PaymentRepository;
import net.starwifi.duobao.service.*;
import net.starwifi.duobao.utils.StringUtils;
import net.starwifi.duobao.utils.times.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigDecimal;
import java.util.Date;


@Service
public class PaymentServiceImpl extends BaseServiceImpl<Payment,Long> implements PaymentService {

    @Autowired
    PaymentRepository repository;

    @Autowired
    OrderService orderService;

    @Autowired
    DBOrderService dbOrderService;

    @Autowired
    TradeRecordService tradeRecordService;
    @Autowired
    GglUserService gglUserService;

    @Autowired
    SettingService settingService;

    @Override
    public Payment findBySn(String sn) {
        return repository.findBySn(sn);
    }

    @Override
    public Payment findByOrderSn(String orderSn) {
        return repository.findByOrderSn(orderSn);
    }


    @Transactional
    public void handle(Payment payment){
        if (payment != null && payment.getStatus() == Payment.Status.wait) {
//            Setting setting = settingService.findByCode("GglMoney");
//            int GglMoney = Integer.parseInt(setting.getConfigValue());
//            if(payment.getAmount().intValue()>=GglMoney){
//                gglUserService.consume(memberService.find(payment.getMemberId())); //充值获取刮刮卡次数奖励
//            }
            if(payment.getType()== Payment.Type.db){  //夺宝订单
                DBOrder order=dbOrderService.findBySn(payment.getOrderSn());
                if(order.getPaymentStatus()==0){
                    dbOrderService.payment(order, payment);
                }
            }else if(payment.getType()== Payment.Type.recharge){ //充值
                tradeRecordService.recharge(payment.getMemberId(),payment.getAmount(),"账户充值",payment.getSn());
            }



            payment.setStatus(Payment.Status.success); //收款单状态变更
            payment.setPaymentDate(new Date());
            update(payment);
        }
    }


//    @Override
//    public void exportTxtFile(File file) throws IOException {
//
//        if(!file.getParentFile().exists()){
//            file.getParentFile().mkdirs();
//        }
//        if(!file.exists()) {
//            file.createNewFile();
//        }
//
////        Date endData=  DateTool.setHms(DateTool.addDays(new Date(), +1), 0, 0, 0);
////        Date beginData =DateTool.setHms(new Date(), 0, 0, 0);
//
//        Date endData= DateTool.setHms(new Date(), 0, 0, 0);
//        Date beginData = DateTool.setHms(DateTool.addDays(new Date(), -1), 0, 0, 0);
//        RandomAccessFile rf=new RandomAccessFile(file,"rw");
//        PageView<Order> pageView=new PageView<Order>();
////        pageView.addFilter(Filter.eq("status", Payment.Status.success));
//        pageView.addFilter(Filter.gt("modifyDate", beginData));
//        pageView.addFilter(Filter.lt("modifyDate", endData));
////        pageView.addFilter();
//        pageView.setNumPerPage(1000);
//        Long total=super.getCount();
//        int totalPage=(int) Math.ceil((double) total / (double) 1000); //总页数
//
//        for(int i=1;i<=totalPage;i++){
//            pageView.setPageNumber(i); //当前页
//            Page<Payment> page = super.getScrollData(pageView);
//            StringBuffer sb=new StringBuffer();
//            for(Payment payment:page.getContent()){
//                if(payment.getSn()!=null&&!payment.getStatus().equals(Payment.Status.wait)&&!payment.getFlag().equals(DBProduct.FLAG_YX)){
//                    Member member =memberService.find(payment.getMemberId());
//                    //合作方交易流水|合作方ID|付款方账户ID|收款方账户ID|服务号码|交易类型|交易金额|交易结果|交易时间
//                    sb.append(payment.getOrderSn()); //合作方交易流水
//                    sb.append("|"+"100004");//|合作方ID
//                    sb.append("|" + member.getMobile());//|付款方账户ID
//                    sb.append("|"+"app_baoxb");//|收款方账户ID
//                    sb.append("|"+member.getMobile());//|服务号码
//                    if(payment.getStatus().equals(Payment.Status.success)){
//                        sb.append("|0");//|交易类型
//                        sb.append("|"+payment.getAmount().multiply(new BigDecimal(100)).intValue());//|交易金额
//                        sb.append("|0");//|交易结果
//                    }
//                    if (payment.getStatus().equals(Payment.Status.failure)){
//                        sb.append("|0");//|交易类型
//                        sb.append("|"+payment.getAmount().multiply(new BigDecimal(100)).intValue());//|交易金额
//                        sb.append("|1");//|交易结果
//                    }
//               Refunds refunds=     refundsService.findByOrderSn(payment.getOrderSn());
//
//                    sb.append("|"+ StringUtils.formatDate(payment.getModifyDate())+"\r\n");//|交易时间
//                    //将数据输出到文件
//
//                }
//            }
////            }
//            rf.writeBytes(sb.toString());
//            rf.seek(rf.length());
//        }
//        rf.close();//关闭文件流
//    }
    @Autowired
    MemberService memberService;
    @Autowired
    RefundsService refundsService ;


}
